package cn.fjw.webservice.servlet;

import cn.fjw.webservice.DataSource;
import cn.fjw.webservice.GlobalVar;
import cn.fjw.webservice.model.City;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Statement;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class CityServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new Gson();
        response.setContentType("application/json; charset=UTF-8");
        String parent = request.getParameter("parent");
        if(parent!=null){
            City province= this.getProvince(Integer.parseInt(parent));
            response.getWriter().println(gson.toJson(province));
        }else {
            response.getWriter().println(gson.toJson(this.getAllCities()));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    List<City> getAllCities(){
        DataSource dataSource = new DataSource();
        Connection connection = dataSource.getConnection();
        List<City> cities = new ArrayList<City>();
        ResultSet resultSet=null;
        Statement statement = null;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from city order by lv asc ");
            while (resultSet.next()){
                City city = new City();
                city.setId(resultSet.getInt("id"));
                city.setName(resultSet.getString("name"));
                city.setPostCode(resultSet.getString("postCode"));
                city.setAbbreviation(resultSet.getString("abbr"));
                city.setEnglishName(resultSet.getString("englishName"));
                city.setLevel(resultSet.getInt("lv"));
                if(resultSet.getInt("parent")==0){
                    city.setParent(null);
                    cities.add(city);
                }else{
                    int parentId = resultSet.getInt("parent");
                    Iterator<City> ite = cities.iterator();
                    while (ite.hasNext()){
                        City parent=ite.next();
                        if(parent.getId()==resultSet.getInt("parent")){
                            city.setParent(parent);
                            parent.getChildren().add(city);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cities;
    }

    City getProvince(Integer id){
        DataSource dataSource = new DataSource();
        Connection connection = dataSource.getConnection();
        List<City> cities = new ArrayList<City>();
        City province = null;
        ResultSet resultSet=null;
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement("select * from city where id=? or parent=? order by lv asc ");
            statement.setInt(1,id);
            statement.setInt(2,id);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                City city = new City();
                city.setId(resultSet.getInt("id"));
                city.setName(resultSet.getString("name"));
                city.setPostCode(resultSet.getString("postCode"));
                city.setAbbreviation(resultSet.getString("abbr"));
                city.setEnglishName(resultSet.getString("englishName"));
                city.setLevel(resultSet.getInt("lv"));
                if(resultSet.getInt("parent")==0){
                    city.setParent(null);
                    cities.add(city);
                    province = city;
                }else{
                    int parentId = resultSet.getInt("parent");
                    Iterator<City> ite = cities.iterator();
                    while (ite.hasNext()){
                        City parent=ite.next();
                        if(parent.getId()==resultSet.getInt("parent")){
                            city.setParent(parent);
                            parent.getChildren().add(city);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return province;
    }
}
